/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import data.CombinationGenerator;
import model.DiagramModel;
import diagram.Graphical;
import diagram.connections.HandleConnection;
import diagram.connections.TriggerConnection;
import diagram.handles.Handle;
import diagram.handles.TriggerHandle;
import diagram.locations.Location;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;

/**
 *
 * @author Jelle
 */
public class ConnectAction extends AbstractAction {

    private DiagramModel model;
    private int modus;
    private Location source, target;
    private Handle h;

    public ConnectAction(DiagramModel model) {
        this.model = model;
        modus = 0;
    }

    public ConnectAction(String s, Handle h, Location target,DiagramModel model) {
        this.model = model;
        if (s.equals("all")) {
            modus = 0;
        } else if (s.equals("trigger")) {
            modus = 1;
            this.h =h;
            this.source = h.getLocation();
            this.target = target;
        }

    }

    public void actionPerformed(ActionEvent e) {
        switch (modus) {
            case 0: System.out.println("ConnectAction modus 0 (all)");
                if (model.getSelection("all").size() > 1) {
                    ArrayList<Graphical> g = model.getSelection("handle");
                    int[] indices;
                    CombinationGenerator x = new CombinationGenerator(g.size(), 2);
                    while (x.hasMore()) {
                        indices = x.getNext();
                        model.addObject(new TriggerConnection((TriggerHandle) g.get(indices[0]), (TriggerHandle) g.get(indices[1])));
                    }
                }

            case 1:System.out.println("ConnectAction modus 1 (trigger)");
                TriggerConnection tc = new TriggerConnection(source, target, model);
                tc.setLevel(15);
                model.addObject(tc);
                model.removeObject(h);
                model.setAttachable(false);

        }


    }
}
